using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Server;
using TXLooker.Core;
using TXLooker.Properties;

namespace TXLooker.UI
{
    public partial class ServerListEditForm : Form
    {
        private ServerListEditForm()
        {
            InitializeComponent();

            this.m_lvServers.SelectedIndexChanged += 
                delegate 
                {
                    if( this.m_lvServers.SelectedItems.Count == 0) return;
                    this.m_btnRemoveServer.Enabled = !( this.m_lvServers.SelectedItems[0].Tag as ServerConfigItem ).IsTeamExplorer;
                };
        }
        public ServerListEditForm( IEnumerable<ServerConfigItem> servers ) : this()
        {
            foreach( ServerConfigItem serverItem in servers )
                AddServerItemToUI( serverItem );
        }

        public IEnumerable<ServerConfigItem> Servers
        {
            get 
            {
                foreach( ListViewItem item in this.m_lvServers.Items )
                    yield return (ServerConfigItem)item.Tag;
            }
        }


        private void OnAdd( object sender, EventArgs e )
        {
            AddTFServerForm form = new AddTFServerForm();

            if( form.ShowDialog() != DialogResult.OK )
                return;

            ServerConfigItem addServerItem = form.ServerItem;

            foreach( ServerConfigItem serverItem in this.Servers )
            {
                if( serverItem.ServerURI != addServerItem.ServerURI )
                    continue;

                MessageBox.Show( this, "Server already exists.", Resources.APPLICATION_NAME );
                return;
            }

            IEnumerable<ProjectInfo> projects = TeamFoundationDataProvider.GetProjects( addServerItem.ServerURI ); ;

            if( projects != null )
            {
                addServerItem.ProjectURIs.Clear();

                foreach( ProjectInfo project in projects )
                    addServerItem.ProjectURIs.Add( project.Uri );
 
            }
            else
            {
                MessageBox.Show( this, Resources.SERVER_NOT_AVAILABLE, Resources.APPLICATION_NAME );
            }

            this.AddServerItemToUI( addServerItem );

            ////check connection to server and get all projects for new added server
            //WorkItemStore store = null;

            //Cursor prevCursor = this.Cursor;
            //this.Cursor = Cursors.WaitCursor;

            //try   { store = ApplicationGlobal.TFDataProvider.GetStore( addServerItem.ServerURI ); }
            //catch { }

            //this.Cursor = prevCursor;

            //if( store != null )
            //{ 
            //    lock( (store.Projects as ICollection).SyncRoot )
            //    {
            //        foreach( Project project in store.Projects )
            //            addServerItem.ProjectURIs.Add( project.Uri.ToString() );
            //    }
            //}
            //else
            //{ 
            //    MessageBox.Show( this, Resources.SERVER_NOT_AVAILABLE, Resources.APPLICATION_NAME );
            //}

            ////show new added server in GUI
            //this.AddServerItemToUI( addServerItem );
        }
        private void OnRemove( object sender, EventArgs e )
        {
            if( this.m_lvServers.SelectedItems.Count == 0 )
                return;

            DialogResult dr = MessageBox.Show( Resources.DELETE_MSG, Resources.APPLICATION_NAME, MessageBoxButtons.YesNo );
            if( dr != DialogResult.Yes ) return;

            foreach( ListViewItem item in this.m_lvServers.SelectedItems )
                this.m_lvServers.Items.Remove( item );
        }
        private void OnClose( object sender, EventArgs e )
        {
            this.Close();
        }

        private void AddServerItemToUI( ServerConfigItem serverItem )
        {
            Uri uri = new Uri(serverItem.ServerURI);

            ListViewItem item = new ListViewItem();
            item.Tag = serverItem;

            ListViewItem.ListViewSubItem subitem1 = new ListViewItem.ListViewSubItem(item, uri.Host);
            item.SubItems.Add( subitem1 );

            ListViewItem.ListViewSubItem subitem2 = new ListViewItem.ListViewSubItem(item, serverItem.ServerURI);
            item.SubItems.Add( subitem2 );

            this.m_lvServers.Items.Add(item);
        }

    }
}